package com.linkage.jz.bankruptcy.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.MyProInfo;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;


public class ProjectBaseAdapter extends BaseRecyclerAdapter<ProjectBaseAdapter.ViewHolder> {

    /**
     * 类型
     */
    public static int PROJECT_PUBLIC = 1;
    public static int PROJECT_MINE = 2;
    int type;

    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();

    List<MyProInfo> mList;
    Context mcontext;



    public ProjectBaseAdapter(Context context, List<MyProInfo> mList, int type) {
        this.mcontext = context;
        this.mList = mList;
        this.type = type;
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mcontext).inflate(R.layout.item_my_pro, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        MyProInfo model = mList.get(position);

        String logo = model.getProjectLogo();
        String proName = model.getProjectName();

        holder.btItemBt1.setVisibility(View.GONE);
        holder.btItemBt2.setVisibility(View.GONE);
        if (type == PROJECT_PUBLIC) {
            showPublic(holder, position, model);
        } else {
            showMine(holder, position, model);
        }

        if ("".equalsIgnoreCase(logo)) {
            if (proName.length() > 2) {
                holder.proTitle.setText(proName.substring(0, 2));
            }
        } else {
            holder.proTitle.setText("");
        }

        Glide.with(mcontext).load(logo)
                .placeholder(R.drawable.nopic)
                .error(R.drawable.nopic).into(holder.tvItemBgaswipeImg);

        holder.tvItemBgaswipeDetail.setText(model.getProjectName());

        holder.silItemBgaswipeRoot.setSwipeAble(false);
        holder.silItemBgaswipeRoot.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
    }

    public void showMine(final ViewHolder holder, int position, MyProInfo model) {
        String strStatus = "";

        holder.btItemBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(v, position, holder);
            }
        });
        holder.btItemBt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(v, position, holder);
            }
        });
        holder.btItemBt1.setVisibility(View.INVISIBLE);
        if ("0".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "暂存";
            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.black));
            holder.btItemBt2.setVisibility(View.VISIBLE);
            holder.btItemBt2.setBackgroundResource(R.drawable.pro_g_corner_btn);
            holder.btItemBt2.setText("继续编辑");
            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_h_corner_btn);
            holder.btItemBt1.setText("删除");
        } else if ("1".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "已关闭";
            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.darker_gray));
        }  else if ("3".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "已结案";
            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.darker_gray));
        }else if ("2".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "程序中";
            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.page_top));
            if (!"".equalsIgnoreCase(model.getManagerId())) {
                holder.btItemBt1.setVisibility(View.VISIBLE);
                holder.btItemBt1.setBackgroundResource(R.drawable.pro_g_corner_btn);
                holder.btItemBt1.setText("管理项目");
            }
        }

        if (model.getAuth().getAllowJoin()) {
            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_o_corner_btn);
            holder.btItemBt1.setText("申请加入");
        }
        if (model.getAuth().getAcceptOrReject()) {
            holder.btItemBt2.setVisibility(View.VISIBLE);
            holder.btItemBt2.setBackgroundResource(R.drawable.pro_o_corner_btn);
            holder.btItemBt2.setText("接收邀请");

            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_h_corner_btn);
            holder.btItemBt1.setText("拒绝");
        }
        if (model.getAuth().getFillingIn()) {
            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_g_corner_btn);
            holder.btItemBt1.setText("填写基础信息");
        }

        if (model.getAuth().getContinueFillingIn()) {
            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_g_corner_btn);
            holder.btItemBt1.setText("继续填写基础信息");
        }

        holder.tvItemStatus.setText(strStatus);
    }

    public void showPublic(final ViewHolder holder, int position, MyProInfo model) {
        String strStatus = "";
        holder.btItemBt1.setBackgroundResource(R.drawable.pro_o_corner_btn);
        holder.btItemBt1.setText("申请加入");
        holder.btItemBt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(v, position, holder);
            }
        });
//        if ("0".equalsIgnoreCase(model.getProjectStatus())) {
//            strStatus = "暂存";
//            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.black));
//            if (model.getProjectColumnList().getAllowJoin())
//                holder.btItemBt1.setVisibility(View.VISIBLE);
//        } else if ("1".equalsIgnoreCase(model.getProjectStatus())) {
//            strStatus = "已关闭";
//            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.darker_gray));
//            if ("true".equalsIgnoreCase(model.getAllowJoin()))
//                holder.btItemBt1.setVisibility(View.VISIBLE);
//        } else if ("2".equalsIgnoreCase(model.getProjectStatus())) {
//            strStatus = "程序中";
//            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.page_top));
//            if ("true".equalsIgnoreCase(model.getAllowJoin()))
//                holder.btItemBt1.setVisibility(View.VISIBLE);
//        } else if ("3".equalsIgnoreCase(model.getProjectStatus())) {
//            strStatus = "已结案";
//            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.darker_gray));
//        }
//        holder.tvItemStatus.setText(strStatus);
        holder.btItemBt1.setVisibility(View.INVISIBLE);
        if ("0".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "暂存";
            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.black));
            holder.btItemBt2.setVisibility(View.VISIBLE);
            holder.btItemBt2.setBackgroundResource(R.drawable.pro_g_corner_btn);
            holder.btItemBt2.setText("继续编辑");
            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_h_corner_btn);
            holder.btItemBt1.setText("删除");
        } else if ("1".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "已关闭";
            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.darker_gray));
        }  else if ("3".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "已结案";
            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.darker_gray));
        }else if ("2".equalsIgnoreCase(model.getProjectStatus())) {
            strStatus = "程序中";
            holder.tvItemStatus.setTextColor(mcontext.getResources().getColor(R.color.page_top));
            if (!"".equalsIgnoreCase(model.getManagerId())) {
                holder.btItemBt1.setVisibility(View.VISIBLE);
                holder.btItemBt1.setBackgroundResource(R.drawable.pro_g_corner_btn);
                holder.btItemBt1.setText("管理项目");
            }
        }

        if (model.getAuth().getAllowJoin()) {
            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_o_corner_btn);
            holder.btItemBt1.setText("申请加入");
        }
        if (model.getAuth().getAcceptOrReject()) {
            holder.btItemBt2.setVisibility(View.VISIBLE);
            holder.btItemBt2.setBackgroundResource(R.drawable.pro_o_corner_btn);
            holder.btItemBt2.setText("接收邀请");

            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_h_corner_btn);
            holder.btItemBt1.setText("拒绝");
        }
        if (model.getAuth().getFillingIn()) {
            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_g_corner_btn);
            holder.btItemBt1.setText("填写基础信息");
        }

        if (model.getAuth().getContinueFillingIn()) {
            holder.btItemBt1.setVisibility(View.VISIBLE);
            holder.btItemBt1.setBackgroundResource(R.drawable.pro_g_corner_btn);
            holder.btItemBt1.setText("继续填写基础信息");
        }

        holder.tvItemStatus.setText(strStatus);
    }


    @Override
    public int getItemCount() {
        return mList != null ? mList.size() : 0;
    }

    class ViewHolder extends BaseHolder {

        private BGASwipeItemLayout silItemBgaswipeRoot;
        private TextView etItemBgaswipeRead;
        private TextView tvItemBgaswipeDelete;
        private ImageView tvItemBgaswipeImg;
        private TextView proTitle;
        private TextView tvItemBgaswipeDetail;
        private TextView tvItemStatus;
        private TextView btItemBt1;
        private TextView btItemBt2;
        private TextView tvItemBgaswipeTime;

        public ViewHolder(View view) {
            super(view);
            silItemBgaswipeRoot = (BGASwipeItemLayout) view.findViewById(R.id.sil_item_bgaswipe_root);
            etItemBgaswipeRead = (TextView) view.findViewById(R.id.et_item_bgaswipe_read);
            tvItemBgaswipeDelete = (TextView) view.findViewById(R.id.tv_item_bgaswipe_delete);
            tvItemBgaswipeImg = (ImageView) view.findViewById(R.id.tv_item_bgaswipe_img);
            proTitle = (TextView) view.findViewById(R.id.pro_title);
            tvItemBgaswipeDetail = (TextView) view.findViewById(R.id.tv_item_bgaswipe_detail);
            tvItemStatus = (TextView) view.findViewById(R.id.tv_item_status);
            btItemBt1 = (TextView) view.findViewById(R.id.bt_item_bt1);
            btItemBt2 = (TextView) view.findViewById(R.id.bt_item_bt2);
            tvItemBgaswipeTime = (TextView) view.findViewById(R.id.tv_item_bgaswipe_time);
        }
    }

}